package com.xy.breakfast.controller;

import com.xy.breakfast.entity.Admin;
import com.xy.breakfast.entity.Clients;
import com.xy.breakfast.service.AdminService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;
import java.util.logging.Logger;

@Controller
public class AdminController {

    @Autowired
    AdminService adminService;
    //日志
    java.util.logging.Logger logger = Logger.getLogger(this.getClass().getName());

    //管理员登录功能
    @RequestMapping("/adminLogin")
    @ResponseBody
    public String adminLogin(@RequestBody Map<String,String> maps, HttpServletRequest request){
        logger.info("管理员开始登录了");

        String username=maps.get("username");
        String password=maps.get("password");

        logger.info("登录用户："+username);
        logger.info("登录密码:"+password);

        if(username.length()==0) return "请输入用户名";
        Admin admin=this.adminService.findByUsername(username);
        if(admin==null) return "邮箱不存在";
        if(!admin.getPassword().equals(password)) return "密码错误";

        //保证网站登录30分组
        request.getSession().setAttribute("admin",admin);

        logger.info("管理员登录成功！");

        return "ok";
    }

    //退出登录
    @RequestMapping("adminUnlogin")
    @ResponseBody
    public String adminUnlogin(HttpServletRequest request){
        request.getSession().setAttribute("admin",null);
        return "ok";
    }
}
